package class5;

import common.TreeNode;
import common.TreeNodeUtils;

/**
 * 判断一棵树是否为镜面树
 */
public class Code03_isMirrorTree {
    public static void main(String[] args) {
        TreeNode treeNode1 = TreeNodeUtils.createTreeNode(1, 2, 2);
        TreeNode left = treeNode1.left;
        TreeNode right = treeNode1.right;
        TreeNode[] treeNodes = TreeNodeUtils.getTreeNodes(4, 5, 5, 4);
        left.left = treeNodes[0];
        left.right = treeNodes[1];
        right.left = treeNodes[2];
        right.right = treeNodes[3];
        System.out.println(treeNode1);

        boolean mirrorTree = isMirrorTree(treeNode1, treeNode1);
        System.out.println("mirrorTree = " + mirrorTree);
    }


    // 判断两个树结构是否相等
    public static boolean isMirrorTree(TreeNode p, TreeNode q) {
        // 两个有一个为空
        if (p == null ^ q == null) {
            return false;
        }
        if (p == null && q == null) {
            return true;
        }
        return p.val == q.val && isMirrorTree(p.left, q.right) && isMirrorTree(p.right, q.left);
    }
}
